<?php
/**
 * bll login
 * @package app-member_lib_bll_member
 */
load_lib('/bll/member/base');
load_lib('/dao/member/logindao');
/**
 * bll login
 * @author pandajingjing
 * @package app-member_lib_bll_member
 */
class bll_member_login extends bll_member_base{

	/**
	 * 注册用户
	 * @param string $p_strUserName
	 * @param string $p_strUserPwd
	 * @param string $p_strUserPwdCfm
	 * @param string $p_strIP
	 * @param int $p_intTime
	 * @return array/int
	 */
	function regUser($p_strUserName,$p_strUserPwd,$p_strUserPwdCfm,$p_strIP,$p_intTime){
		$bolErrFnd=false;
		$arrErrInfo=array();
		load_lib('/util/member');
		$mixResult=util_member::chkUserName($p_strUserName);
		if(true!==$mixResult){
			$bolErrFnd=true;
			$arrErrInfo[]=$mixResult;
		}
		if(isset($p_strUserPwd[0]) and isset($p_strUserPwdCfm[0])){
			if($p_strUserPwd!==$p_strUserPwdCfm){
				$bolErrFnd=true;
				$arrErrInfo[]='Err_Logic_UserPwd_NotConfirm';
			}
		}else{
			$bolErrFnd=true;
			$arrErrInfo[]='Err_Logic_UserPwd_NoValue';
		}
		if($bolErrFnd){
			return $arrErrInfo;
		}
		if($this->chkUserInfoExist($p_strUserName,'USERNAME')){
			$bolErrFnd=true;
			$arrErrInfo[]='Err_Logic_UserName_Exist';
		}
		if($bolErrFnd){
			return $arrErrInfo;
		}
		$strCryptPwd=$this->generateCryptPwd($p_strUserPwd,$p_strUserName);
		$arrUserBaseInfo=array( 
				'USERNAME'=>$p_strUserName,
				'USERPWD'=>$strCryptPwd,
				'REGIP'=>$p_strIP,
				'REGTIME'=>$p_intTime
		);
		$intNewUserID=dao_member_logindao::addUserBase($arrUserBaseInfo);
		$arrRegMsg=array( 
				'MSGTYPE'=>1,
				'USERID'=>$intNewUserID,
				'USERNAME'=>$p_strUserName,
				'USERPWD'=>$strCryptPwd,
				'REGIP'=>$p_strIP,
				'REGTIME'=>$p_intTime
		);
		dao_member_logindao::addUserQueue($arrRegMsg);
		return $intNewUserID;
	}

	/**
	 * 用户登录
	 * @param string $p_strLoginValue
	 * @param string $p_strUserPwd
	 * @param string $p_strIP
	 * @param string $p_intTime
	 * @param array $o_arrUserInfo
	 * @return int/array
	 */
	function loginUser($p_strLoginValue,$p_strUserPwd,$p_strIP,$p_intTime,&$o_arrUserInfo){
		$bolErrFnd=false;
		$arrErrInfo=array();
		load_lib('/util/member');
		$mixResultUserName=util_member::chkUserName($p_strLoginValue);
		if(true===$mixResultUserName){
			$strLoginType='username';
		}else{
			$mixResultEmail=util_member::chkEmail($p_strLoginValue);
			if(true===$mixResultEmail){
				$strLoginType='email';
			}else{
				$bolErrFnd=true;
				$arrErrInfo[]='Err_Logic_LoginType_NotMatch';
			}
		}
		if(!isset($p_strUserPwd[0])){
			$bolErrFnd=true;
			$arrErrInfo[]='Err_Logic_UserPwd_NoValue';
		}
		if($bolErrFnd){
			return $arrErrInfo;
		}
		$arrUserInfo=dao_member_logindao::getUserLoginInfo($p_strLoginValue,$strLoginType);
		if(null===$arrUserInfo){
			$bolErrFnd=true;
			$arrErrInfo[]='Err_Logic_UserInfo_NoExist';
		}
		if($bolErrFnd){
			return $arrErrInfo;
		}
		$strCryptPwd=$this->generateCryptPwd($p_strUserPwd,$arrUserInfo['USERNAME']);
		if($strCryptPwd!=$arrUserInfo['USERPWD']){
			$bolErrFnd=true;
			$arrErrInfo[]='Err_Logic_UserPwd_NotMatch';
		}
		if($bolErrFnd){
			return $arrErrInfo;
		}else{
			$arrLoginMsg=array( 
					'MSGTYPE'=>2,
					'USERID'=>$arrUserInfo['USERID'],
					'LOGINIP'=>$p_strIP,
					'LOGINTIME'=>$p_intTime 
			);
			dao_member_logindao::addUserQueue($arrLoginMsg);
			$o_arrUserInfo=array(
					'USERID'=>$arrUserInfo['USERID'],
					'USERNAME'=>$arrUserInfo['USERNAME'],
					'LASTUPD'=>$arrUserInfo['LASTUPD']
			);
			$this->addLog(array( 
					'USERID'=>$arrUserInfo['USERID'],
					'IP'=>$p_strIP 
			),'user_login');
			return $arrUserInfo['USERID'];
		}
	}

}